# Template file for 'coreutils'
pkgname=coreutils
version=9.4
revision=20
bootstrap=yes
makedepends="gmp-devel acl-devel libcap-devel"
short_desc="GNU core utilities"
maintainer="MarcoAPC <marcoaureliopc@gmail.com>"
license="GPL-3.0-or-later"
homepage="https://www.gnu.org/software/coreutils"
changelog="https://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=blob_plain;f=NEWS;hb=HEAD"
distfiles="${GNU_SITE}/coreutils/coreutils-${version}.tar.xz"
checksum=ea613a4cf44612326e917201bbbcdfbd301de21ffc3b59b6e5c07e040b275e52
alternatives="
 hostname:hostname:/usr/bin/hostname-coreutils
 hostname:hostname.1:/usr/share/man/man1/hostname-coreutils.1"
replaces="chroot-coreutils>=0 coreutils-doc>=0 b2sum>=0"

if [ "$CHROOT_READY" ]; then
	hostmakedepends+=" perl"
	# allow run ./configure by root in ethereal chroot
	if [ "$XBPS_ALLOW_CHROOT_BREAKOUT" ]; then
		_force_unsafe_configure="FORCE_UNSAFE_CONFIGURE=1"
	fi
fi

if [ "$XBPS_TARGET_LIBC" = musl ]; then
	export ac_cv_lib_error_at_line=no
	export ac_cv_header_sys_cdefs_h=no
fi

pre_configure() {
	# Build natively all utils for the host, we need this to generate
	# the manpages via help2man.
	if [ "$CROSS_BUILD" ]; then
		env CC=cc LD=ld AR=ar RANLIB=ranlib \
			CFLAGS=-Os CXXFLAGS=-Os LDFLAGS= \
			$_force_unsafe_configure \
			./configure --prefix=${wrksrc}/coreutils-${XBPS_MACHINE} \
			--enable-install-program=arch,hostname \
			--enable-no-install-program=kill,uptime
		make ${makejobs}
		make install
		make distclean
	fi
}

do_configure() {
	if [ "$CROSS_BUILD" ]; then
		configure_args+=" fu_cv_sys_stat_statfs2_bsize=no
			gl_cv_func_working_mkstemp=yes
			gl_cv_func_working_acl_get_file=yes "
	fi
	case "$XBPS_TARGET_MACHINE" in
		# XXX syncfs() in src/sync.c expects a return value.
		*-musl) configure_args+=" ac_cv_func_syncfs=no";;
	esac

	# (y2k38) This can be removed once musl is updated to 1.2.x
	if [ "$XBPS_TARGET_LIBC" = "musl" ] && [ "$XBPS_TARGET_WORDSIZE" = "32" ]; then
		configure_args+=" --disable-year2038"
	fi

	#
	# Do not install kill: provided by util-linux.
	# Do not install uptime: provided by procps-ng.
	#
	env $_force_unsafe_configure ./configure ${configure_args} \
		--enable-install-program=arch,hostname \
		--enable-no-install-program=kill,uptime \
		--disable-rpath
	if [ "$CROSS_BUILD" ]; then
		# Use install when cross compiling
		cp Makefile Makefile.cfg.orig
		sed -e 's;^\(cu_install_program =\).*;\1 install;' \
			Makefile.cfg.orig >Makefile
	fi
}

do_build() {
	if [ "$CROSS_BUILD" ]; then
		cp Makefile Makefile.orig
		sed '/src_make_prime_list/d' Makefile.orig > Makefile
		depbase=$(echo src/make-prime-list.o | sed 's|[^/]*$|.deps/&|;s|\.o$||')
		cc -std=gnu99  -I. -I./lib  -Ilib -I./lib -Isrc -I./src  \
			-fdiagnostics-show-option -funit-at-a-time -g -O2 -MT \
			src/make-prime-list.o -MD -MP -MF $depbase.Tpo -c \
			-o src/make-prime-list.o \
			src/make-prime-list.c
		mv -f $depbase.Tpo $depbase.Po
		cc -std=gnu99 -fdiagnostics-show-option -funit-at-a-time -g -O2 \
			-Wl,--as-needed  -o src/make-prime-list src/make-prime-list.o
		cp Makefile Makefile.bak
		sed -e '/hostname.1/d' Makefile.bak > Makefile
	fi
	make ${makejobs}
}

do_check() {
	# Tests that fail due to being inside a chroot
	vsed -i '/tests\/chgrp/d' Makefile

	local exeext_tests
	exeext_tests="chown lchown fchownat"

	for test in $exeext_tests ; do
		vsed -i "s/test-$test\$(EXEEXT) //" gnulib-tests/Makefile
	done

	if [ "$XBPS_BUILD_ENVIRONMENT" = "void-packages-ci" ]; then
		vsed -i '/tests\/tail\/inotify-dir-recreate/d' Makefile
		vsed -i '/tests\/du\/long-from-unreadable/d' Makefile
		vsed -i '/tests\/dd\/no-allocate/d' Makefile
	fi

	make check
}

do_install() {
	make DESTDIR=${DESTDIR} install
	if [ "$CROSS_BUILD" ]; then
		mv ${wrksrc}/coreutils-${XBPS_MACHINE}/share/man \
			${DESTDIR}/usr/share
		# provided by procps-ng
		rm -f ${DESTDIR}/usr/share/man/man1/{kill,uptime}.1
	fi
	mv ${DESTDIR}/usr/bin/hostname ${DESTDIR}/usr/bin/hostname-coreutils
	mv ${DESTDIR}/usr/share/man/man1/hostname.1 ${DESTDIR}/usr/share/man/man1/hostname-coreutils.1

	ln -s test.1 "${DESTDIR}/usr/share/man/man1/[.1"
}
